<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>检查坏死链接</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="AboutFayland.html">Fayland</a>&nbsp;&nbsp;>>Next: <a href="DBI.html">DBI</a></p>
<h1>检查坏死链接</h1>
<div class='content'>
<p>Category: <a href='Script.html'>Script</a> &nbsp; Keywords: <b>broken</b></p>特殊说明：此文档只为检查本地文档，用于某人在本机内某目录内的相互链接。目前只支持HTML文档。<a href="../scripts/CheckBrokenLink.pl.txt">Download</a><br>
如果需要检查整个网站的Broken Link,推荐使用软件<a href="http://home.snafu.de/tilman/xenulink.html" target="_blank">XENU</a><br>
如果需要一个Perl文件来检查整个网站的Broken Link，请参考Steven McDougall's <a href="http://world.std.com/~swmcd/steven/perl/pm/lc/linkcheck.html">Checking links with LinkCheck</a>

<pre>
#!/usr/bin/perl
<span class="comment"># cbl.pl for Check Bad Links.</span>
use strict;

@ARGV or die "Usage:perl cbl.pl [E:\directory|/usr/html]";

my $dir = $ARGV[0];
(-e "$dir" && -d "$dir") or die "$dir doesn't exist or not a directory!";
$dir .= "/" if ($dir !~ /(\/|\\)$/);

<span class="comment">#define</span>
my @file;
my %Err;

&GetDir("$dir", \@file);

foreach (@file) {
    &cbl("$dir$_");
}
if (keys %Err) {
    print "Errors are\n";
    foreach (keys %Err) {
        print "$_ has\n\t$Err{$_}\n";
    }
} else {
    print "All is OK!"
}

sub cbl {
    my ($file) = @_;
    return if ($file !~ /\.html?$/);
    my $dir = $file;
    $dir =~ s/([^(\/|\\)]*)$//;
    local $/;
    open(FH,"$file");
    my $html = <FH>;
    close(FH);
    while ($html =~ s/href\=(\S+?)(\>|\s)//i) {
        my $tmp = $1;
        $tmp =~ s/(\"|\')?(\S+?)(\"|\')?/$2/;
        next if ($tmp =~ /^(https?\:\/\/|mailto\:|\#)/);
        unless (-e "$dir$tmp") {
            $Err{"$file"} .= "$tmp\n\t";
        }
    }
}

sub GetDir {
    my ($dir, $file_ref, $subdir) = @_;
    if (($subdir ne "") && ($subdir !~ /\/$/)) { $subdir = "$subdir/"; }
    opendir(DIRS, "$dir");
    my @dirdata = readdir(DIRS);
    closedir(DIRS);
    foreach (@dirdata) {
        next if (/^\.+$/);
        if (-d "$dir/$_") {
            &GetDir("$dir/$_", $file_ref, "$subdir$_");
        } else {
            push (@$file_ref, "$subdir$_");
        }
    }
}
</pre></div></div>
<p><&lt;Previous: <a href="AboutFayland.html">Fayland</a>&nbsp;&nbsp;>>Next: <a href="DBI.html">DBI</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E6%A3%80%E6%9F%A5%E5%9D%8F%E6%AD%BB%E9%93%BE%E6%8E%A5&url=http://www.fayland.org/journal/CheckBadLinks.html'>+Del.icio.us</a></p>

Created on <span class="digit">2004-10-30 18:48:44</span>, Last modified on <span class="digit">2005-06-02 01:53:19</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>